Skip to main content

touch command

touch - Create empty files or update the timestamps of existing files.

Changes the shell working directory.

Usage: touch [OPTION]... [FILE]...

  • OPTION: Flags which enhances the touch abilities.
  • FILE: The file you want to create or modify.

Examples

  • Creating a new file

    The most common use of touch is to create an empty file if it doesn’t already exist.

    $ touch myfile.txt
    • Creates an empty file named myfile.txt in the current directory.
    • If myfile.txt already exists, its last modified timestamp is updated to the current time.
  • Creating multiple files

    You can create multiple files at once by listing them.

    $ touch file1.txt file2.txt file3.txt
    • Creates three empty files: file1.txt, file2.txt, and file3.txt.
    • If file1.txt, file2.txt, and file3.txt already exists, its last modified timestamp is updated to the current time.
  • Setting a Specific Timestamp

    We can set a custom timestamp using the -t option, instead of using the current time.

    $ touch -t 202306060600.00 myfile.txt
    • Sets the timestamp to December 25, 2023, 12:30:00.
    • Format: YYYYMMDDHHMM.SS (year, month, day, hour, minute, seconds).
  • Only update access time

    Use -a to update only the access timestamp (when the file was last read).

    $ touch -a myfile.txt
    • Changes the access time without affecting the modification time.
  • Only update modification time

    Use -m to update only the modification timestamp (when the file was last changed).

    $ touch -m myfile.txt
    • Changes the modification time without affecting the access time.
  • Copy the timestamp from another file as a reference file with the -r (reference) option.

    $ touch -r source.txt target.txt
    • Sets the timestamp of target.txt to match source.txt
  • Preventing file creation

    Use -c (no-create) to update timestamps only if the file exists, without creating it.

    $ touch -c nonexistent.txt
    • Does nothing since nonexistent.txt doesn’t exist.
$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit

Note that the -d and -t options accept different time-date formats.